package com.woniuxy.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.entity.Model;
import com.woniuxy.entity.ModelItem;
import com.woniuxy.entity.vo.VModelItem;
import com.woniuxy.entity.vo.VModelItem1;
import com.woniuxy.entity.vo.VSpec;
import com.woniuxy.mapper.ModelItemMapper;
import com.woniuxy.service.ModelItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.hash.HashMapper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ModelItemServiceImpl extends ServiceImpl<ModelItemMapper, ModelItem> implements ModelItemService {
    @Autowired
    private ModelItemMapper modelItemMapper;
    /**
    * @description: 根据模板ID获取模板详情
     * @param id:
     * @return: java.util.List<com.woniuxy.entity.ModelItem>
     * @date: 2021/8/18 16:40
     * @author: 魏炳豪
     */
    @Override
    public List<ModelItem> getByModelId(int id) {
        List<ModelItem> modelItems = modelItemMapper.getByModelId(id);
        return modelItems;
    }

    /**
    * @description: 根据模型id删除模型详情
     * @param id:
     * @return: void
     * @date: 2021/8/18 17:24
     * @author: 魏炳豪
     */
    @Override
    public void deleteByModelId(int id) {
        modelItemMapper.deleteByModelId(id);
    }

    /**
     * 返回将规格名和规格值的键值对的Map
     * @return
     */
    @Override
    public Map<String, String> getBean() {

        List<ModelItem> modelItems = modelItemMapper.getAll();
        HashMap<String, String> beans = new HashMap<>();
        for (ModelItem modelItem : modelItems) {
            String specValue = modelItem.getSpecValue();

            String sp = specValue.substring(1, specValue.length()-1);

            String[] speces = sp.split(",");
            for (String spece : speces) {
                String[] split = spece.split(":");
                beans.put(split[0].substring(1,split[0].length()-1),split[1].substring(1,split[1].length()-1));
            }
        }

        return beans;
    }
    /**
     * 返回获取封装了id,specName,specValue的List
     * @return List<VModelItem>
     */
    @Override
    public List<VModelItem> getVModelItems() {
        List<VModelItem> vModelItems = new ArrayList<>();

        List<ModelItem> modelItems = modelItemMapper.getAll();
        for (ModelItem modelItem : modelItems) {
            VModelItem vModelItem = new VModelItem();

            List<VSpec> vSpecs = new ArrayList<>();


            //传入id
            vModelItem.setId(modelItem.getId());
            //传入规格名
            vModelItem.setSpecName(modelItem.getSpecName());
            //取规格值键值对
            String specValue = modelItem.getSpecValue();

            String sp = specValue.substring(1, specValue.length()-1);

            String[] speces = sp.split(",");
            for (String spece : speces) {
                String[] split = spece.split(":");
                VSpec vSpec = new VSpec();
                vSpec.setValue(split[0].substring(1,split[0].length()-1));
                vSpec.setPrice(split[1].substring(1,split[1].length()-1));
                vSpecs.add(vSpec);

            }
            vModelItem.setSpecValue(vSpecs);
            vModelItems.add(vModelItem);
        }

        return vModelItems;
    }

    @Override
    public List<VModelItem1> getVModelItems1ByModelId(Integer modelId) {
        List<VModelItem1> vModelItem1s = new ArrayList<>();

        List<ModelItem> modelItems = modelItemMapper.getByModelId(modelId);
        for (ModelItem modelItem : modelItems) {
            VModelItem1 vModelItem1 = new VModelItem1();

            List<String> vSpecs = new ArrayList<>();


            //传入id
            vModelItem1.setId(modelItem.getId());
            //传入规格名
            vModelItem1.setSpecName(modelItem.getSpecName());
            //取规格值键值对
            String specValue = modelItem.getSpecValue();


            String[] speces = specValue.split(",");

            for (String spece : speces) {

                vSpecs.add(spece);

            }
            vModelItem1.setSpecValue(vSpecs);
            vModelItem1s.add(vModelItem1);
        }

        return vModelItem1s;
    }
}
